package com.homework.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.homework.dao.CourseUtil;
import com.homework.doamin.Course;

public class CourseAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int currentPage=1;
		request.setCharacterEncoding("UTF-8");
		CourseUtil courseUtil=new CourseUtil();
		String courseAcronym=request.getParameter("course");
		Course course=courseUtil.getCourseByAcronym(courseAcronym);
		int total=courseUtil.getTotalNumberOfHomework(course);
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.valueOf(((String)request.getParameter("currentPage")));
			if(Integer.valueOf(request.getParameter("currentPage"))<=0){
				currentPage=1;
			}
			if(Integer.valueOf(request.getParameter("currentPage"))>total/5+1){
				currentPage=total/5+1;
			}
		}
		course.setHomeworks(courseUtil.getHomeworkItem(course,currentPage));
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("lastPage", total/5+1);
		request.setAttribute("course", course);
//		System.out.println(servlet.getServletContext().getRealPath(""));
//		System.out.println("\\");
		//System.out.println(servlet.getServletConfig().getServletContext().getRealPath(""));
		// TODO Auto-generated method stub
		return mapping.findForward("chooseOut");
	}
	
}
